<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:insert="~{include :: header('优惠卷种类信息列表')}" />
    <th:block th:insert="~{include :: bootstrap-table-css}" />
</head>
<body class="hold-transition">
<div class="container-fluid mb-2">
    <div class="toolbar" id="toolbar" role="group">
        <form id="formId" class="form-inline">
            <a class="btn btn-success mr-1" onclick="$.operate.add();">
                <i class="fa fa-plus"></i> 添加
            </a>
            <input type="text" class="form-control mr-1" name="searchKey" placeholder="请输入优惠卷名称...">
            <select name="useType" class="form-control mr-1" th:with="type=${@dict.getType('sys_coupon_number')}">
                <option value="">类型</option>
                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
            </select>
            <button type="button" class="btn btn-primary mr-1" onclick="$.table.search()">
                <i class="fa fa-search"></i>
                搜索
            </button>
            <button type="button" class="btn btn-default" onclick="$.form.reset()">
                <i class="fa fa-refresh"></i>
                重置
            </button>
        </form>
    </div>
    <table id="bootstrap-table" class="table-striped"></table>
</div>
<th:block th:insert="~{include :: footer}" />
<th:block th:insert="~{include :: bootstrap-table-js}" />
<script th:inline="javascript">
    var userTypes = [[${@dict.getType('sys_coupon_number')}]];
    var prefix = "/coupon";

    $(function() {
        var options = {
            url: prefix + "/list.json",
            createUrl: prefix + "/add.html",
            removeUrl: prefix + "/remove.json",
            modalName: "优惠卷种类信息",
            columns: [
                {
                    field: 'id',
                    title: 'id'
                },
                {
                    field: 'title',
                    title: '优惠卷名称'
                },
                {
                    field: 'useType',
                    align: 'center',
                    title: '使用类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(userTypes, value);
                    }
                },
                {
                    field: 'amount',
                    align: 'center',
                    title: '抵扣金额',
                    formatter: function (value, row, index) {
                        return row.amount / 100;
                    }
                },
                {
                    field: 'startDate',
                    align: 'center',
                    title: '有效期',
                    formatter: function (value, row, index) {
                        return row.startDate + " ~ " + row.endDate;
                    }
                },
                {
                    field: 'totalNum',
                    align: 'center',
                    title: '可使用次数'
                },
                {
                    field: 'useNum',
                    align: 'center',
                    title: '已使用数量'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    formatter: function (value, row, index) {
                        return row.createTime;
                    }
                },
                {
                    field: 'managerName',
                    title: '创建人'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        var number = parseInt(row.totalNum) - parseInt(row.userNum);
                        if (number == 0) {
                            actions.push('<a class="btn btn-success btn-xs disabled" href="javascript:void(0)" ><i class="fa fa-shopping-bag"></i> 赠送</a> ');
                        } else {
                            actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="giveCoupons(\'' + row.id + '\')" ><i class="fa fa-shopping-bag"></i> 赠送</a> ');
                        }
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="couponDetail(\'' + row.id + '\')"><i class="fa fa-search"></i> 详情</a> ');
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="couponRemove(\'' + row.id + '\')"><i class="fa fa-remove"></i> 删除</a>');
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    //优惠券详情
    function couponDetail(id) {
        layer.open({
            type: 2,
            area: ['85%', '95%'],
            fix: false,
            maxmin: true,
            shade: 0.3,
            title: "详情",
            content: "/coupon/code/list.html?id="+id
        });
    }

    //删除优惠券
    function couponRemove(id) {
        table.set();
        $.modal.confirm("确定删除将无法恢复，确认删除吗？", function () {
            var data = {"id": id};
            $.operate.submit(prefix + "/remove.json", "post", "json", data);
        });
    }

    //赠送优惠券
    function giveCoupons(couponId) {
        layer.open({
            type: 2,
            area: ['600px', '230px'],
            fix: false,
            maxmin: true,
            shade: 0.3,
            title: "赠送优惠券",
            content: "/coupon/give.html?id="+couponId
        });
    }

    //赠送优惠券保存操作
    function togiveCoupon() {
        var userId = $("#userId").val();
        var data = {"userId": userId, "couponId": couponId};
        $.operate.submit("/coupon/give.json", "post", "json", data, function (json) {
            //console.log(json)
            if (json.code === 0) {
                $("#username").val("");
                $("#clossTable").click();
                $.modal.msgSuccess("赠送成功")
            }
        });
    }
</script>
</body>
</html>